$(function() {
    $("#mini_style_row li").click(function() {
        $("#mini_style_row li").removeClass("active");
        $(this).addClass("active");
        var data = $(this).attr("data");
        $("#mini_style_1").hide();
        $("#mini_style_2").hide();
        $("#mini_style_"+data).show();
        showPage();
    });

    $("#btn_animat_row li").click(function() {
        $("#btn_animat_row li").removeClass("active");
        $(this).addClass("active");
        showPage();
    });

    $("#btn_style_row li").click(function() {
        $("#btn_style_row li").removeClass("active");
        $(this).addClass("active");
        showPage();
    });

    showPage();
});

function showPage() {
    var style = $("#mini_style_row .active").attr("data");
    $("#phone_mini_style_1").hide();
    $("#phone_mini_style_2").hide();
    $("#phone_mini_style_"+style).show();
    var mini_name = $("#mini_name").val();
    var mini_describe = $("#mini_describe").val();
    $("#phone_mini_style_1 h3").text(mini_name);
    $("#phone_mini_style_1 h5 font").text(mini_describe);
    var mini_logo = $("#mini_logo").val();
    $("#phone_mini_style_1 img").attr("src",mini_logo);
    var btn_style = $("#btn_style_row .active").attr("data");
    var btn_animat = $("#btn_animat_row .active").attr("data");
    var btn_text = $("#btn_text").val();
    var btn_color = $("#btn_color").val();
    var btnHtml = '';
    $(".alimini-btn-default").remove();
    $(".alimini-btn-fixed").remove();
    if (btn_style == 1) {
        btnHtml += '<div class="alimini-btn-default">';
    } else {
        btnHtml += '<div class="alimini-btn-fixed">';
    }
    if (btn_animat == 2) {
        btnHtml += '<a href="javascript:;" class="alimini-btn beat" style="background-color:'+btn_color+';">'+btn_text+'</a></div>';
    } else {
        btnHtml += '<a href="javascript:;" class="alimini-btn" style="background-color:'+btn_color+';">'+btn_text+'</a></div>';
    }
    var imageLists = JSON.parse($("#image_list").val());
    $(".image-lists").html("");
    $.each(imageLists, function(index,imageList) {
        $(".image-lists").append('<img src="'+imageList+'"/>');
    });
    $("#phone_mini_style_1").append(btnHtml);
    $("#phone_mini_style_2").append(btnHtml);
}

function jsUploadCallBack(id,imageurl,imageName,updata) {
    showPage();
}

function jsImagesListsDeleteCallback() {
    showPage();
}

function miniListUpdate() {
    var link_id = $("#link_id").val();
    var name = $("#name").val();
    var appid = $("#appid").val();
    var path = $("#path").val();
    var mini_name = $("#mini_name").val();
    var mini_logo = $("#mini_logo").val();
    if (name == "") {
        mviewToastr("error","请输入链接名称！");
        $("#name").focus();
        return;
    }
    if (appid == "") {
        mviewToastr("error","请输入小程序APPID！");
        $("#appid").focus();
        return;
    }
    if (path == "") {
        mviewToastr("error","请输入小程序路径！");
        $("#path").focus();
        return;
    }
    var mini_style = $("#mini_style_row .active").attr("data");
    var btn_style = $("#btn_style_row .active").attr("data");
    var btn_animat = $("#btn_animat_row .active").attr("data");
    var btn_text = $("#btn_text").val();
    var btn_color = $("#btn_color").val();
    var image_list = $("#image_list").val();
    var mini_logo = $("#mini_logo").val();
    var mini_describe = $("#mini_describe").val();
    mviewToastr("load","创建中,请稍等...");
    $.ajax({
        url: "/admin/alipay/mini_update",
        type: "POST",
        data: {
            "link_id":link_id,
            "name":name,
            "appid":appid,
            "path":path,
            "mini_name":mini_name,
            "mini_style":mini_style,
            "mini_logo":mini_logo,
            "mini_describe":mini_describe,
            "btn_style":btn_style,
            "btn_animat":btn_animat,
            "btn_text":btn_text,
            "btn_color":btn_color,
            "image_list":JSON.stringify(image_list),
        },
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        success:function(result){
            if (result.status == "success") {
                mviewToastr("success",result.msg);
                setTimeout(function(){
                    window.location.href = "/admin/alipay/mini_detial/"+result.data.link_id;
                }, 2000);
            } else {
                mviewToastr("error",result.msg);
            }
        },
        error:function(err){
            if (err.status == 419) {
                mviewToastr("error","页面已过期,请刷新网页后重新保存！");
            } else {
                mviewToastr("error","网络异常,请联系客服处理！");
            }
        }
    })
}

function wechatService() {
    $("#wechatService").click();
}